﻿

Namespace Processing
    Public Class DeleteAssemblyCache
        Inherits BaseProcess(Of DeleteAssemblyCache)
        Implements Interfaces.IProcess

        Public Sub Action() Implements Interfaces.IProcess.Action
            CleanAssemblyCache()
        End Sub

        Public ReadOnly Property Name As String Implements Interfaces.IProcess.Name
            Get
                Return "Cleaning Assembly Cache"
            End Get
        End Property
        Private Shared Sub CleanAssemblyCache()
            Dim appDataDirectory As String
            Dim systemUserDirectory As String
            Dim myDocuments As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            If (Environment.OSVersion.Version.Major > 5) Then
                appDataDirectory = "{0}:\Users\{1}\AppData\Local\assembly\dl3"
                systemUserDirectory = myDocuments.Remove(0, myDocuments.LastIndexOf("Users\")).Replace("\Documents", "").Replace("Users\", "")
            Else
                appDataDirectory = "{0}:\Documents and Settings\{1}\Local Settings\assembly"
                systemUserDirectory = myDocuments.Remove(0, myDocuments.LastIndexOf("Documents and Settings\")).Replace("\My Documents", "").Replace("Documents and Settings\", "")
            End If
            Dim systemDriveLetter As String = Environment.SystemDirectory.Substring(0, 1)
            Dim rootDir As String = String.Format(appDataDirectory, systemDriveLetter, systemUserDirectory)
            For x As Integer = 1 To 4
                DeleteAssemblyCache.DeleteDirectoryPass(rootDir)
            Next

        End Sub

        Private Shared Sub DeleteDirectoryPass(ByVal rootDir As String)
            If My.Computer.FileSystem.DirectoryExists(rootDir) Then
                My.Computer.FileSystem.GetDirectories(rootDir).AsParallel.ForAll(Sub(deldir)
                                                                                     Try
                                                                                         My.Computer.FileSystem.DeleteDirectory(deldir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                                                                                     Catch ex As Exception
                                                                                         ' Do nothing just skip
                                                                                     End Try
                                                                                 End Sub)
            End If
        End Sub

    End Class
End Namespace

